package controllers.tran;

import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.apache.commons.collections.CollectionUtils;

import play.db.jpa.JPA;
import play.mvc.With;

import com.pany.cache.UserNameCache;
import com.pany.domain.IConstant;
import com.pany.domain.IConstant.ICommonResultCode;
import com.pany.domain.IConstant.IDictType;
import com.pany.domain.IConstant.ITranResultCode;
import com.pany.domain.models.TranType;
import com.pany.domain.models.User;
import com.pany.domain.req.AddTranTypeReq;
import com.pany.domain.req.GetTranTypeListReq;
import com.pany.domain.req.ModifyTranTypeReq;
import com.pany.domain.req.ModifyTranTypeStatusReq;
import com.pany.domain.rsp.AddTranTypeRsp;
import com.pany.domain.rsp.GetTranTypeRsp;
import com.pany.exceptions.ServiceException;
import com.pany.utils.DictUtils;
import com.pany.utils.ServiceUtils;
import com.pany.utils.UUIDGenerator;

import controllers.BaseController;
import controllers.interceptor.UserSecure;
import controllers.user.UserController;

/**
 * 消费项目Controller
 * 
 * @author 曾杰
 * @since 2012-10-30/V0.1
 */
@With(value = {UserSecure.class})
public class TranTypeController extends BaseController
{
    public static void getTranTypeList()
    {
        GetTranTypeListReq req = readObject(GetTranTypeListReq.class);
        int total = ServiceUtils.queryCountByCondition(req);
        GetTranTypeRsp rsp = new GetTranTypeRsp();
        
        if (total > 0)
        {
            rsp.setTotalNum(total);
            List<TranType> list = (ServiceUtils.queryByCondition(req, TranType.class));
            rsp.setTypeList(list);
            if (CollectionUtils.isNotEmpty(list))
            {
                for (TranType tranType : list)
                {
                    tranType.setCreatorName(UserNameCache.getInstance().getCache(tranType.getCreator()));
                    tranType.setLastUpdateUserName(UserNameCache.getInstance().getCache(tranType.getLastUpdateUserId()));
                    tranType.setStatusName(DictUtils.getDictName(IDictType.STATUS_NAME_DICTTYPE, tranType.getStatus()));
                }
            }
        }
        renderSuccess(rsp);
    }
    
    /**
     * 添加消费项信息
     */
    public static void addTranType()
    {
        AddTranTypeReq req = new AddTranTypeReq();
        
        TranType type = req.getTranType();
        if (type == null)
        {
            throw new ServiceException(ICommonResultCode.PARAMTER_IS_NULL, "tanType");
        }
        User user = getCurrentUser();
        String tid = UUIDGenerator.generate();
        type.setTranTypeId(tid);
        type.setCreator(user.getUserId());
        type.setLastUpdateUserId(user.getUserId());
        type.setLastUpdateTime(new Date());
        type.setCreateTime(new Date());
        
        if (type.getDiscount() == null || type.getDiscount().doubleValue() == 0)
        {// 如果折扣没有填则为不打折
            type.setDiscount(10D);
        }
        
        type.setStatus(IConstant.TRAN_TYPE_STATUS_ENABLE);
        
        type.save();
        
        AddTranTypeRsp rsp = new AddTranTypeRsp();
        rsp.setTranTypeId(tid);
        
        renderSuccess(rsp);
    }
    
    /**
     * 修改消费项的状态
     */
    public static void modifyTranTypeStatus()
    {
        ModifyTranTypeStatusReq req = readObject(ModifyTranTypeStatusReq.class);
        if (req.getStatus() == null)
        {
            throw new ServiceException(ICommonResultCode.PARAMTER_IS_NULL, "status");
        }
        if (req.getTranTypeId() == null)
        {
            throw new ServiceException(ICommonResultCode.PARAMTER_IS_NULL, "tranTypeId");
        }
        Query q = JPA.em().createQuery("update TranType set status=? where tranTypeId=?");
        q.setParameter(1, req.getStatus());
        q.setParameter(2, req.getTranTypeId());
        q.executeUpdate();
        renderSuccess(null);
    }
    
    /**
     * 修改消费项的详细信息
     */
    public static void modifyTranType()
    {
        ModifyTranTypeReq req = readObject(ModifyTranTypeReq.class);
        if (req.getTranType() == null)
        {
            throw new ServiceException(ICommonResultCode.PARAMTER_IS_NULL, "tranType");
        }
        Query q = JPA.em().createQuery("from TranType where tranTypeId=?", TranType.class);
        List<TranType> list = q.getResultList();
        if (CollectionUtils.isEmpty(list))
        {
            throw new ServiceException(ITranResultCode.TRAN_TYPE_NOT_EXITS);
        }
        TranType old = list.get(0);
        if (IConstant.TRAN_TYPE_STATUS_DISABLE.equals(old.getStatus()))
        {
            throw new ServiceException(ITranResultCode.TRAN_TYPE_NOT_EXITS);
        }
        old.setLastUpdateTime(new Date());
        old.setLastUpdateUserId(getCurrentUser().getUserId());
        old.setPrice(req.getTranType().getPrice());
        old.setDiscount(req.getTranType().getDiscount());
        old.setRemark(req.getTranType().getRemark());
        old.setTranName(req.getTranType().getTranName());
        old.save();
        renderSuccess(null);
    }
}
